package backstage.sys.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import backstage.util.pojo.Page;

public class SystemDao extends HibernateDaoSupport {

	protected Page getPageCount(String hql,int pageSize){
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		List result = query.list();
		session.close();
		Page p = new Page();
		p.setPageSize(pageSize);
		p.setFirstPage(1);
		p.setPrevPage(1);
		p.setCurPage(1);
		p.setHasPrePage(false);
		int count = ((Long)result.get(0)).intValue();
		if(count == 0)count = 1;
		p.setCount(count);
		int z = count / pageSize;
		int y = count % pageSize;
		if(y == 0){
			p.setCountPage(z);
			p.setLastPage(z);
			if(z == 1 || z == 0){
				p.setNextPage(1);
				p.setHasNextPage(false);
			}else{
				p.setNextPage(2);
				p.setHasNextPage(true);
			}
		}else{
			p.setCountPage(z + 1);
			p.setLastPage(z + 1);
			if(z == 0){
				p.setNextPage(1);
				p.setHasNextPage(false);
			}else{
				p.setNextPage(2);
				p.setHasNextPage(true);
			}
		}
		return p;
	}
}
